//
//  ZYCTeaLeaveRecordSendDetailViewController.m
//  ZDSC
//
//  Created by 张威 on 2021/11/2.
//

#import "ZYCTeaLeaveRecordSendDetailViewController.h"
#import "ZYCTeaLeaveRecordDetailCell.h"
#import "ZYCTeaLeaveAddPhotoCell.h"
#import "ZYCTeaLeaveOptionalCell.h"
#import "ZYCManageCourseTeachTimeCell.h"
#import "ZYCTeaLeaveRecordDetailHeaderView.h"
#import "ZYCTeaLeaveRecordFlowChartView.h"
#import "ZYCTeaLeaveRecordRootModel.h"
#import "ZYCTeaLeaveFlowChartModel.h"

@interface ZYCTeaLeaveRecordSendDetailViewController ()<UITableViewDataSource,UITableViewDelegate,ZYCTeaLeaveAddPhotoCellDelegate>

@property (nonatomic, strong) ZYCTeaLeaveRecordDetailHeaderView *headerView;
@property (nonatomic, strong) ZYCTeaLeaveRecordFlowChartView *flowChartView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *dataSource;
@property (nonatomic, strong) NSDictionary *headerDict;
@property (nonatomic, strong) ZYCTeaLeaveRecordRootModel *detailModel;
@end

@implementation ZYCTeaLeaveRecordSendDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"请假详情";
    [self.view addSubview:self.tableView];
    self.tableView.tableHeaderView = self.headerView;
    self.tableView.tableFooterView = self.flowChartView;
    [self loadTeaLeaveDetial];
}
#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 2) {
        return 3;
    }
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 3) {// 请假事由
        ZYCTeaLeaveOptionalCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZYCTeaLeaveOptionalCell class]) forIndexPath:indexPath];
        cell.isHid = YES;
        cell.titleString = self.dataSource[indexPath.section];
        NSString *leaveReason = nil;
        if (self.detailModel.leaveReason.length > 0) {
            leaveReason = self.detailModel.leaveReason;
        }else{
            leaveReason = @"无";
        }
        cell.contentString = leaveReason;
        return cell;
    }else if(indexPath.section == 4){// 上传图片 ** 显示图片
        ZYCTeaLeaveAddPhotoCell *cell = [ZYCTeaLeaveAddPhotoCell teacherLeaveAddPhotoCellWithType:ZYCTeaLeaveAddPhotoTypeShow TableView:tableView];
        cell.titleString = self.dataSource[indexPath.section];
        cell.dataSource = self.detailModel.files.mutableCopy;
        cell.delegate = self;
        return cell;
    }else if(indexPath.section == 2 && indexPath.row != 0){
        ZYCManageCourseTeachTimeCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZYCManageCourseTeachTimeCell class]) forIndexPath:indexPath];
        if (indexPath.row == 1) {
            cell.contentString = self.detailModel.leaveStartTime;
        }else{
            cell.contentString = self.detailModel.leaveEndTime;
        }
        return cell;
    }else{
        ZYCTeaLeaveRecordDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZYCTeaLeaveRecordDetailCell class]) forIndexPath:indexPath];
        cell.titleLabel.text = self.dataSource[indexPath.section];
        switch (indexPath.section) {
            case 0:{// 所属部门
                cell.contentLabel.text = self.detailModel.deptName;
                break;
            }
            case 1:{// 所属岗位
                cell.contentLabel.text = self.detailModel.postName;
                break;
            }
            case 2:{// 类型/时间
                cell.contentLabel.text = [NSString stringWithFormat:@"%@/%@天",self.detailModel.leaveTypeName,self.detailModel.leaveDays];
                break;
            }
            default:
                break;
        }
        return cell;
    }
    
}

-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 5.0f;
}

-(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 3) {
        return 150.0f;
    }else if(indexPath.section == 4){
        return 150.0f;
    }
    return 55.0f;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}


#pragma mark - ZYCTeaLeaveAddPhotoCellDelegate
- (void)teaLeaveBrowserWithIndex:(NSInteger)index PhotoCell:(ZYCTeaLeaveAddPhotoCell *)cell{
    NSMutableArray *browserDataArr = [NSMutableArray array];
    NSArray <ZYCTeaLeaveRecordFileModel *>*filesArray = cell.dataSource;
    [filesArray enumerateObjectsUsingBlock:^(ZYCTeaLeaveRecordFileModel * _Nonnull fileModel, NSUInteger idx, BOOL * _Nonnull stop) {
        YBIBImageData *data = [YBIBImageData new];
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/zd-system-service%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"sdfz_url"],fileModel.filePath]];
        data.imageURL = url;
        [browserDataArr addObject:data];
    }];
    YBImageBrowser *browser = [YBImageBrowser new];
    browser.dataSourceArray = browserDataArr;
    browser.currentPage = index;
    [browser show];
}


#pragma mark - getters
- (ZYCTeaLeaveRecordDetailHeaderView *)headerView {
    if (!_headerView) {
        _headerView = [[ZYCTeaLeaveRecordDetailHeaderView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 80.0f)];
    }
    return _headerView;
}

- (ZYCTeaLeaveRecordFlowChartView *)flowChartView {
    if (!_flowChartView) {
        _flowChartView = [[ZYCTeaLeaveRecordFlowChartView alloc] init];
        _flowChartView.type = ZYCTeaLeaveFlowChartOther;
    }
    return _flowChartView;
}


- (NSArray *)dataSource {
    if (!_dataSource) {
        _dataSource = @[@"所属部门:",@"所属岗位:",@"类型/时间:",@"请假事由",@"上传图片"];
    }
    return _dataSource;
}


- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight, kScreenWidth, kScreenHeight - kNavBarAndStatusBarHeight) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = HEX_RGB(@"#FAFAFA");
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[ZYCTeaLeaveRecordDetailCell class] forCellReuseIdentifier:NSStringFromClass([ZYCTeaLeaveRecordDetailCell class])];
        [_tableView registerClass:[ZYCTeaLeaveOptionalCell class] forCellReuseIdentifier:NSStringFromClass([ZYCTeaLeaveOptionalCell class])];
        [_tableView registerClass:[ZYCTeaLeaveAddPhotoCell class] forCellReuseIdentifier:NSStringFromClass([ZYCTeaLeaveAddPhotoCell class])];
        [_tableView registerClass:[ZYCManageCourseTeachTimeCell class] forCellReuseIdentifier:NSStringFromClass([ZYCManageCourseTeachTimeCell class])];
        _tableView.showsVerticalScrollIndicator = NO;
    }
    return _tableView;
}

#pragma mark - 网络请求
- (void)loadTeaLeaveDetial{
    NSDictionary *params = @{
        @"teacherLeaveId":self.teacherLeaveId,
    };
    [MrjSVProgressHUD showWithStatus:@"加载中..."];
    [[URLManager sharedURLManager] postUrlString:Appliance_Post_GetInfo requestService:RequestServiceActiviti parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(id  _Nullable responseObject) {
            if (SUCCESS(responseObject[@"code"])) {
                self.detailModel = [ZYCTeaLeaveRecordRootModel yy_modelWithDictionary:responseObject[@"body"]];
                self.headerView.model = self.detailModel;
                
                [self getflowChart];
            }else{
                [MrjToast makeToast:responseObject[@"message"] makeView:self.view];
            }
        } failure:^(NSError * _Nonnull error) {
            
        }];
}

- (void)getflowChart{
    NSDictionary *params = @{
        @"teacherLeaveId":self.teacherLeaveId,
    };
    [[URLManager sharedURLManager] postUrlString:Appliance_Post_History requestService:RequestServiceActiviti parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(id  _Nullable responseObject) {
        [MrjSVProgressHUD dismiss];
        if (SUCCESS(responseObject[@"code"])) {
            NSArray *array = [NSArray yy_modelArrayWithClass:[ZYCTeaLeaveFlowChartModel class] json:responseObject[@"body"]];
            for (ZYCTeaLeaveFlowChartModel *model in array) {
                model.isShow = YES;
            }
            self.flowChartView.frame = CGRectMake(0, 0, kScreenWidth, 120.0f * array.count + 50.0f);
            ZYCTeaLeaveFlowChartModel *last = array.lastObject;
            last.isShow = NO;
            self.flowChartView.dataSource = array;
            [self.flowChartView reload];
            [self.tableView reloadData];
        }
    } failure:^(NSError * _Nonnull error) {
        
    }];
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
